<html xmlns:AI>
<head>
<?import namespace="AI" implementation="aiTree.htc" />
<link href="dialog.css" rel="stylesheet" type="text/css">
<title>Insert Image</title>
<script>
	var d = document;
	var pic = dialogArguments;
	var http;
	var doc;
			
	function init() {
		if( pic.get ) {
			http = new ActiveXObject( 'Msxml2.XMLHTTP' );
			http.open( 'GET', pic.get, true );
			http.onreadystatechange = function() {
				if( http.readyState == 4 ) {
					doc = new ActiveXObject( 'Microsoft.XMLDOM' );
					doc.loadXML( http.responseText );
					if( ! doc.xml ) {					
						doc.loadXML( '<root><pic name="-- no images uploaded --"/></root>' );
					}
					doc.setProperty( 'SelectionLanguage', 'XPath' );
					addOpt( doc.documentElement, 0, '' );
					aiTree.data = doc.xml;
					http = null;
				}
			}
			http.send();
		}
		if( ! pic.put ) {
			put_menu.style.display = 'none';
			aiTree.height = 180;
		}
	}
				
	onload = function() {
		init();
		aiTree.onchoose = function( e ) {
			returnValue = e.href;
			close();
		}
		btnCan.onclick = function() {
			returnValue = null;
			close();
		}
		btnIns.onclick = insert;
	}

	function addOpt( from, lvl, par ) {
		var l = from.selectNodes( "./*[not( @href )]" );
		for( var i = 0; i < l.length; i++ ) {
			var o = d.createElement( '<option>' );
			o.value = par + l[i].getAttribute( 'name' ) + '/';
			for( var j = 0; j < lvl; j++ ) {
				o.text += '   ';
			}
			o.text += l[i].getAttribute( 'name' );
			fol.add( o );
			addOpt( l[i], lvl + 1, o.value );
		}
	}
		
	// event handlers
			
	function insert() {
		doc || ( doc = new ActiveXObject( 'Microsoft.XMLDOM' ) );
		doc.loadXML( '<root xmlns:dt="urn:schemas-microsoft-com:datatypes"/>' );

		var e = doc.createElement( 'file' );
		var s = new ActiveXObject( 'Adodb.Stream' );

		e.setAttribute( 'href', fil.value );
		e.setAttribute( 'folder', fol.value );
		e.setAttribute( 'name', des.value );
		e.dataType = 'bin.base64';
		s.type = 1;

		try {
			s.open();
			s.loadFromFile( fil.value );
			e.nodeTypedValue = s.read();
			doc.documentElement.appendChild( e );
			http = new ActiveXObject( 'Msxml2.Xmlhttp' );
			http.open( 'POST', pic.put, false );
			http.send( doc );
			if( http.status != 200 ) { throw 1; }
//			pic.src = http.responseXml.selectSingleNode( 'root/file' ).getAttribute( 'href' );
			doc.loadXML( http.responseText );
			pic.src = doc.selectSingleNode( 'root/file' ).getAttribute( 'href' );
			returnValue = pic.src;
			close();
		} catch( err ) {
			alert( err.description + 'The file could not upload successfully ! ' );
		} finally {
			http = null;
			s.close();
		}
	}
</script>
</head>
	<body>
		<table cellspacing="0" cellpadding="1">
			<tr>
				<td colspan="3" class="title">:: insert image ::</td>
			</tr>
			<tr id="put_menu">
				<td colspan="3">
					<table cellspacing="0" cellpadding="1">
						<tr>
							<td width="80" align="right">
								file source: 
							</td>
							<td>
								<input id="fil" type="file" size="40" style="text-align:left;">
							</td>
							<td rowspan="3">
								<input id="btnCan" type="button" style="width:75px;text-align:center;" value=":: cancel ::">
								<br><br>
								<input id="btnIns" type="button" style="width:75px;text-align:center;" value=":: upload ::">
							</td>
						</tr>
						<tr>
							<td align="right">
								description : 
							</td>
							<td>
								<input id="des" size="40" style="text-align:left;">
							</td>
						</tr>
						<tr>
							<td align="right">
								folder : 
							</td>
							<td>
								<select id="fol" style="text-align:left;">
								</select>
							</td>
						</tr>
						<tr>
							<td colspan="3"><hr class="line"></td>
						</tr>
					</table>
				</td>
			</tr>
			<tr>
				<td align="right" valign="top">
					repository :
				</td>
				<td colspan="2" valign="top">
					<AI:aiTree id="aiTree" preview="1" />
				</td>
			</tr>
		</table>
	</body>
</html>